package com.example.personalcenter.model;

import android.app.Application;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

public class UserViewModel extends AndroidViewModel {

    private static final String KEY_INTEREST = "interest";
    private final SharedPreferences sp;
    private final MutableLiveData<String> interest = new MutableLiveData<>();

    public UserViewModel(@NonNull Application application) {
        super(application);
        sp = application.getSharedPreferences("user", 0);
        interest.setValue(sp.getString(KEY_INTEREST, "未选择"));
    }

    public LiveData<String> getInterest() {
        return interest;
    }

    public void setInterest(String newInterest) {
        sp.edit().putString(KEY_INTEREST, newInterest).apply();
        interest.setValue(newInterest);
    }
}